package net.zjitc.utils;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * Created by Administrator on 2017/11/30.
 */
public class MailUtils {
    //发送邮件
    /*
    * 1.创建一个会话session
    * 2.创建一个代表邮件对象的Message
    * 3.设置内容
    * */

    public static void sendMail(String toaddress,String code) throws Exception {
        //1.创建一个会话session
        Properties prop=new Properties();
        //邮箱服务器
        prop.setProperty("mail.host","smtp.qq.com");
        //授权
        prop.setProperty("mail.smtp.auth","true");
        //qq邮箱需设置端口号
        prop.setProperty("mail.smtp.port","587");

        Authenticator authenticator=new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                //密码框16位口令
                return new PasswordAuthentication("3425923046@qq.com","pvqnufoxckaadbec");
            }
        };
        Session session=Session.getInstance(prop,authenticator);
        //创建一个代表邮件对象的Message
        Message message=new MimeMessage(session);
        //设置内容
        //发件人地址
        message.setFrom(new InternetAddress("3425923046@qq.com"));
        //收件人地址(TO:直接发送)
        message.addRecipient(Message.RecipientType.TO,new InternetAddress(toaddress));
        //设置标题
        message.setSubject("来自软件");
        //设置发送内容（格式）
        String content = "<a href=\"http://localhost:8080/shoptest/activeCode?code="+code+"\">http://localhost:8080/shoptest/activeCode?code="+code+"</a>";
        message.setContent("请点击下方的链接激活：<br>"+content,"text/html;charset=utf-8");
        //发送
        Transport.send(message);

    }



}
